package com.itheima.test11;

import com.itheima.domain.Student;

import java.util.ArrayList;
import java.util.Scanner;

public class ManagerSystem {
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        ArrayList<Student> students = new ArrayList<>();

        loop : while(true){
            System.out.println("1:添加学生");
            System.out.println("2:删除学生");
            System.out.println("3:修改学生");
            System.out.println("4:查看学生");
            System.out.println("5:谢谢使用");
            String next = scanner.next();
            switch (next){
                case "1":
//                    System.out.println("添加学生");
                    addStudent(students);
                    break;
                case "2":
//                    System.out.println("删除学生");
                    deleteStudent(students);
                    break;
                case "3":
//                    System.out.println("修改学生");
                    changeStudent(students);
                    break;
                case "4":
//                    System.out.println("查看学生");
//                    System.out.println(students);
                    queryStudent(students);
                    break;
                case "5":
                    System.out.println("谢谢使用");
                    break loop;
                default:
                    System.out.println("请输入[1,5]区间的整数");
            }
        }
    }
    //修改学生
    private static void changeStudent(ArrayList<Student> students) {
        System.out.println("请输入修改学生学号");
        Scanner scanner = new Scanner(System.in);
        String id = scanner.next();
        int index = getIndex(students,id);

        if (index == -1){
            System.out.println("输入学号不存在");
            return;
        }else{
            System.out.println("请输入新的学生姓名");
            String name = scanner.next();
            System.out.println("请输入新的学生年龄");
            int age = scanner.nextInt();
            System.out.println("请输入新的学生生日");
            String birth = scanner.next();

            Student newStudent = new Student(id, name, age, birth);
            students.set(index,newStudent);
            System.out.println("修改成功");

        }

    }

    //删除学生
    private static void deleteStudent(ArrayList<Student> students) {
        System.out.println("请输入需要删除学生的学号");
        Scanner scanner = new Scanner(System.in);
        String id = scanner.next();
        int index = getIndex(students,id);
        if (index == -1){
            System.out.println("输入学号不存在");
            return;
        }
        students.remove(index);
    }

    //查询arraylist中是否存在该id
    private static int getIndex(ArrayList<Student> students, String id) {
        int index = -1;
        for (int i = 0; i < students.size(); i++) {
            String id1 = students.get(i).getId();
            if (id1.equals(id)){
                index = i;
            }
        }
        return index;
    }

    //查询学生
    private static void queryStudent(ArrayList<Student> students) {
        if (students.size() == 0){
            System.out.println("暂无数据，欢迎添加");
        }
        for (int i = 0; i < students.size(); i++) {
            System.out.println(students.get(i).toString());
        }
    }

    //添加学生
    private static void addStudent(ArrayList<Student> students) {
        Scanner scanner = new Scanner(System.in);
        String id;
        while (true){
            System.out.println("请输入学生学号");
            id = scanner.next();
            int index = getIndex(students, id);
            if (index == -1){
                break;
            }else{
                System.out.println("该学号已存在，请重新输入");
            }
        }
        System.out.println("请输入学生姓名");
        String name = scanner.next();
        System.out.println("请输入学生年龄");
        int age = scanner.nextInt();
        System.out.println("请输入学生生辰");
        String birth = scanner.next();

        Student student = new Student(id,name,age,birth);

        students.add(student);
        System.out.println("添加成功");


    }
}
